import {taskCommitList } from '@/services/task';

export default {
  namespace: 'taskCommit',

  state: {
    loadAble: true, // 是否可以下拉
    list: []
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(taskCommitList, payload);
      if (response.code === 0 && response.result) {
        yield put({
          type: 'save',
          payload: response.result,
        });
      }else {
        yield put({
          type: 'save',
          payload: [],
        });
        yield put({
          type: 'unLoadAble'
        });
      }
    },
    *fetchNew({ payload }, { call, put }) {
      const response = yield call(taskCommitList, payload);
      if (response.code === 0 && response.result) {
        yield put({
          type: 'saveNew',
          payload: response.result,
        });
      }else {
        yield put({
          type: 'unLoadAble'
        });
        alert("没有记录了！")
      }
    },
  },


  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    saveNew(state, action) {
      const list = state.list;
      return {
        ...state,
        list: list.concat(action.payload),
      };
    },
    unLoadAble(state) {
      return {
        ...state,
        loadAble: false,
      };
    },
  },
};
